﻿using System;
using NUnit.Framework;
using MVCBootstrap.MongoDB.Web.Security;
using System.Web.Security;
using MVCBootstrap.MongoDB;
using MongoDB.Driver;
using System.Web.Mvc;
using System.Configuration.Provider;

namespace MVCBootstrap.Tests.MongoDB {

	[TestFixture]
	public class UserInRoleTests {
		private static MongoDBMembershipProvider userProvider;
		private static MongoDBRoleProvider roleProvider;
		private const String server = "mongodb://localhost";
		private const String database = "mvcbootstraptest";

		#region Initialization and cleanup
		[TestFixtureSetUp]
		public static void TestInitialize() {
			DependencyResolver.SetResolver(new TestResolver());
			userProvider = new MongoDBMembershipProvider();
			userProvider.Initialize("MongoProvider", MembershipTestHelper.GetSettings("MongoProvider"));
			roleProvider = new MongoDBRoleProvider();
			roleProvider.Initialize("MongoProvider", MembershipTestHelper.GetSettings("MongoProvider"));
		}

		[TestFixtureTearDown]
		public static void TestCleanup() {
			// Let's clear the database!
			new MongoClient(server).GetServer().GetDatabase(database).Drop();
		}
		#endregion

		[Test]
		public void AddRoles() {
			String username = "Steen F. Tøttrup";
			MembershipCreateStatus status;
			userProvider.CreateUser(username, "123456", "TEST@test.com", String.Empty, String.Empty, true, null, out status);
			Assert.AreEqual(MembershipCreateStatus.Success, status);

			String roleName = "The role";
			roleProvider.CreateRole(roleName);
			Assert.AreEqual(true, roleProvider.RoleExists("The role"));

			roleProvider.AddUsersToRoles(new String[] { username }, new String[] { roleName });

			Assert.AreEqual(true, roleProvider.IsUserInRole(username, roleName));
		}

		[Test]
		public void RoleListTest() {
			String username1 = "John Doe";
			MembershipCreateStatus status;
			userProvider.CreateUser(username1, "123456", "johndoe@test.com", String.Empty, String.Empty, true, null, out status);

			String username2 = "Jane Doe";
			userProvider.CreateUser(username2, "123456", "janedoe@test.com", String.Empty, String.Empty, true, null, out status);

			String roleName = "The Does";
			roleProvider.CreateRole(roleName);

			roleProvider.AddUsersToRoles(new String[] { username1, username2 }, new String[] { roleName });

			String[] userList = roleProvider.GetUsersInRole(roleName);
			Assert.AreEqual(2, userList.Length);

			userList = roleProvider.FindUsersInRole(roleName, "Doe");
			Assert.AreEqual(2, userList.Length);

			String roleName2 = "The Donts";
			roleProvider.CreateRole(roleName2);

			roleProvider.AddUsersToRoles(new String[] { username1, username2 }, new String[] { roleName2 });

			try {
				roleProvider.DeleteRole(roleName, true);
				Assert.True(false);
			}
			catch (ProviderException) {
			}

			roleProvider.DeleteRole(roleName, false);

			//userList = roleProvider.FindUsersInRole(roleName, "Doe");
			//Assert.AreEqual(0, userList.Length);

			userList = roleProvider.FindUsersInRole(roleName2, "Doe");
			Assert.AreEqual(2, userList.Length);
		}
	}
}